package com.woniuxy.controller;



import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.woniuxy.controller.form.DefectProportionForm;

import com.woniuxy.controller.info.DefectStateProportionInfo;
import com.woniuxy.controller.info.DefectTypeProportionInfo;
import com.woniuxy.controller.info.DefectseverityProportionInfo;
import com.woniuxy.controller.info.TypeDataInfo;
import com.woniuxy.model.Defect;
import com.woniuxy.model.DefectModelInfo;
import com.woniuxy.model.DefectWorkhourDay;
import com.woniuxy.model.DefectWorkmounthday;
import com.woniuxy.service.StatisticAlanalysisService;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.TableStyleInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/StatisticAlanalysis")
@Api(tags = "统计分析接口")
public class StatisticAlanalysisController {

    @Resource
    private StatisticAlanalysisService statisticAlanalysisService;


    /**
     * 查询缺陷分类占比
     * @param defectTypeProportionForm
     * @return
     */
    @ApiOperation("查询缺陷分类占比")
    @PostMapping("/SelectAllDefectType")
    public Result SelectAllDefectType(@RequestBody DefectProportionForm defectTypeProportionForm){

        System.out.println("1111111"+defectTypeProportionForm.getProjectName());

        Date[] defectCreattimes = defectTypeProportionForm.getDefectCreattime();


        String projectName = defectTypeProportionForm.getProjectName();

        List<DefectModelInfo> defectModelInfos = statisticAlanalysisService.selectAlldefectTypeNum(projectName, defectCreattimes);
        return Result.success(200,"success",defectModelInfos);

    }


    /**
     * 查询缺陷严重程度占比
     * @param defectProportionForm
     * @return
     */
    @ApiOperation("查询缺陷严重程度占比")
    @PostMapping("/SelectAllDefectseverity")
    public Result SelectAllDefectseverity(@RequestBody DefectProportionForm defectProportionForm){

        System.out.println("1111111"+ defectProportionForm.getProjectName());

        Date[] defectCreattimes = defectProportionForm.getDefectCreattime();

        //String[] defectCreattime = TimeMethodsutil.getTimeToString(defectCreattimes);
        String projectName = defectProportionForm.getProjectName();
        List<DefectModelInfo> defectModelInfos = statisticAlanalysisService.selectAlldefectseverityNum(projectName, defectCreattimes);
        return Result.success(200,"success",defectModelInfos);

    }

    /**
     * 查询缺陷状态程度占比
     * @param defectProportionForm
     * @return
     */
    @ApiOperation("查询缺陷状态程度占比")
    @PostMapping("/SelectAllDefectstate")
    public Result SelectAllDefectstate(@RequestBody DefectProportionForm defectProportionForm){

        Date[] defectCreattimes = defectProportionForm.getDefectCreattime();
        String projectName = defectProportionForm.getProjectName();
        List<DefectModelInfo> defectModelInfos = statisticAlanalysisService.selectAlldefectstateNum(projectName, defectCreattimes);

        return Result.success(200,"success",defectModelInfos);

    }


    /**
     * 导出登记工时数据
     * @param response
     * @param defectProportionForm
     */
    @ApiOperation("导出登记工时数据")
    @GetMapping(value = "/exportDefectType", produces = "application/octet-stream")
    public void exportToDefectType(HttpServletResponse response, DefectProportionForm defectProportionForm){
        ExportParams params = new ExportParams("登记工时表","登记工时表", ExcelType.HSSF);
        Date[] defectCreattimes = defectProportionForm.getDefectCreattime();

        String projectName = defectProportionForm.getProjectName();

        List<DefectWorkhourDay> allDataofWorkhourDay = statisticAlanalysisService.getAllDataofWorkhourDay(projectName, defectCreattimes);


        Workbook sheets = ExcelExportUtil.exportExcel(params, DefectWorkhourDay.class,allDataofWorkhourDay );

        ServletOutputStream out = null;
        response.setCharacterEncoding("utf-8");
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("登记工时表.xls","UTF-8"));

            out = response.getOutputStream();
            sheets.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    /**
     * 导出登记月报数据
     * @param response
     * @param defectProportionForm
     */
    @ApiOperation("导出登记月报数据")
    @GetMapping(value = "/exportworkmounth", produces = "application/octet-stream")
    public void exportToWorkmounth(HttpServletResponse response, DefectProportionForm defectProportionForm){
        ExportParams params = new ExportParams("登记月报表","登记月报表", ExcelType.HSSF);
        Date[] defectCreattimes = defectProportionForm.getDefectCreattime();

        String projectName = defectProportionForm.getProjectName();

        List<DefectWorkmounthday> workmounthday = statisticAlanalysisService.findWorkmounthday(projectName, defectCreattimes);


        Workbook sheets = ExcelExportUtil.exportExcel(params, DefectWorkmounthday.class,workmounthday );

        ServletOutputStream out = null;
        response.setCharacterEncoding("utf-8");
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("登记月报表.xls","UTF-8"));

            out = response.getOutputStream();
            sheets.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    /**
     * 查找工时数据
     * @param defectProportionForm
     * @return
     */
    @ApiOperation("查找工时数据")
    @PostMapping("/SelectWorkhourday")
    public Result SelectWorkhourday(@RequestBody DefectProportionForm defectProportionForm){

        Date[] defectCreattimes = defectProportionForm.getDefectCreattime();

        String projectName = defectProportionForm.getProjectName();

        List<DefectWorkhourDay> workhourday = statisticAlanalysisService.findWorkhourday(projectName, defectCreattimes);
        return Result.success(200,"success",workhourday);
    }
    /**
     * 查找工时月报数据
     * @param defectProportionForm
     * @return
     */
    @PostMapping("/SelectWorkmounthday")
    public Result SelectWorkmounthday(@RequestBody DefectProportionForm defectProportionForm){

        Date[] defectCreattimes = defectProportionForm.getDefectCreattime();

        String projectName = defectProportionForm.getProjectName();

        List<DefectWorkmounthday> workmounthday = statisticAlanalysisService.findWorkmounthday(projectName, defectCreattimes);
        return Result.success(200,"success",workmounthday);
    }
}
